<?php
//by willson 105328468@qq.com 2014/10/13
//member module interface

class address extends core {
	public $addr_mod;
	public function __construct() {
		parent::__construct();
		$this->addr_mod = m("address");
	}

	//获取默认地址，已经登录的情况下才返回
	public function default_address() {
		$address = $this->addr_mod->default_address();
		success($address);
	}

	//设置默认地址
	public function set_default() {
		$id = (isset($_POST['id']) && intval($_POST['id'])) ? intval($_POST['id']) : 0;
		if ($id<=0) error('数据错误');
		$sql = "UPDATE {$this->tbp}address SET is_default='0' WHERE member_id='{$this->member_id}'";
		$this->db->query($sql);
		$sql = "UPDATE {$this->tbp}address SET is_default='1' WHERE member_id='{$this->member_id}' AND id='{$id}'";
		$this->db->query($sql);
		success('ok');
	}

	//列出所有地址
	public function list_address() {
		$sql = "SELECT * FROM {$this->tbp}address WHERE member_id='{$this->member_id}' ORDER BY id ASC";
		$addresses = $this->db->get_results($sql);
		success($addresses);
	}

	//获取某个地址
	public function get_address() {
		$id = (isset($_GET['id']) && intval($_GET['id'])) ? intval($_GET['id']) : 0;
		$sql = "SELECT * FROM {$this->tbp}address WHERE member_id='{$this->member_id}' AND id='{$id}'";
		$address = $this->db->get_row($sql);
		success($address);
	}

	//写入地址，返回写入地址的ID
	public function add_address() {
		$this->save();
	}

	//编辑地址，包括设置默认地址
	public function edit_address() {
		$this->save();
	}

	//发布、编辑
	public function save() {
		$id = (isset($_POST['id']) && trim($_POST['id'])) ? intval($_POST['id']) : 0;
		$contactman = (isset($_POST['contactman']) && trim($_POST['contactman'])) ? trim($_POST['contactman']) : '';
		$province = (isset($_POST['province']) && trim($_POST['province'])) ? trim($_POST['province']) : '';
		$city = (isset($_POST['city']) && trim($_POST['city'])) ? trim($_POST['city']) : '';
		$district = (isset($_POST['district']) && trim($_POST['district'])) ? trim($_POST['district']) : '';
		$address = (isset($_POST['address']) && trim($_POST['address'])) ? trim($_POST['address']) : '';
		$mobile = (isset($_POST['mobile']) && trim($_POST['mobile'])) ? trim($_POST['mobile']) : '';
		$zipcode = (isset($_POST['zipcode']) && trim($_POST['zipcode'])) ? trim($_POST['zipcode']) : '';
		$idcard = (isset($_POST['idcard']) && trim($_POST['idcard'])) ? trim($_POST['idcard']) : '';
		$is_default = (isset($_POST['is_default']) && trim($_POST['is_default'])) ? intval($_POST['is_default']) : 0;
		if ($is_default>0) {
			$sql = "UPDATE {$this->tbp}address SET is_default='0' WHERE member_id='{$this->member_id}' AND is_default='1'";
			$this->db->query($sql);
		}
		if ($id>0) {
			$sql = "UPDATE {$this->tbp}address SET member_id='{$this->member_id}', contactman='{$contactman}', zipcode='{$zipcode}', idcard='{$idcard}',
				province='{$province}', city='{$city}', district='{$district}', address='{$address}', is_default='{$is_default}', mobile='{$mobile}'
				WHERE id='{$id}' AND member_id='{$this->member_id}'";
			$this->db->query($sql);
		} else {
			$sql = "INSERT INTO {$this->tbp}address(member_id, contactman, province, city, district, address, is_default, mobile, zipcode, idcard)
			VALUES('{$this->member_id}', '{$contactman}', '{$province}', '{$city}', '{$district}', '{$address}', '{$is_default}', '{$mobile}', '{$zipcode}', '{$idcard}')";
			$this->db->query($sql);
			$id = $this->db->insert_id;
		}
		success($id);
	}

	//删除地址
	public function delete_address() {
		$id = (isset($_POST['id']) && intval($_POST['id'])) ? intval($_POST['id']) : 0;
		$sql = "DELETE FROM {$this->tbp}address WHERE member_id='{$this->member_id}' AND id='{$id}'";
		$this->db->query($sql);
		success("ok");
	}
}
